#include <stdafx.h>
#include "ActionsQueuer.h"
//#include "defs.h"
//
//#include <string>
//
//#pragma warning( push )
//#pragma warning( disable : 4211 )
//static UINT __stdcall ActionsThreadFunc(LPVOID lpParam)
//{
//    SAFE_THREAD(reinterpret_cast<CActionsQueuer*>(lpParam), ActionsThread);
//}
//#pragma warning( pop )
//
//void CActionsQueuer::ActionsThread()
//{
//    HANDLE events [] = { *m_actionsQueue.get_event_ptr(), m_hTerminateEvent, m_hExceptionEvent.m_h };
//
//    for ( ; ; )
//    {
//        DWORD dwResMsg = ::WaitForMultipleObjects(
//            sizeof(events) / sizeof(HANDLE),
//            events,
//            false,
//            INFINITE);
//
//        if (dwResMsg == WAIT_OBJECT_0 + 1)
//            break;
//
//        else if (dwResMsg != WAIT_OBJECT_0)
//            throw std::runtime_error(std::string(__FUNCTION__) + "WaitForMultipleObjects failed");
//
//        for ( ; ; )
//        {
//            rdp_lib::Buffer packet;
//            if (m_actionsQueue.pop_swap(packet))
//                m_pCallback->DoAction(&packet);
//            else
//                break;
//
//            if (::WaitForSingleObject(m_hTerminateEvent, 0) == WAIT_OBJECT_0)
//                return;
//        }
//    }
//}
//
//CActionsQueuer::CActionsQueuer
//(
//    rdp_lib::utils::queued_action::IActionCallback* pCallback,
//    rdp_lib::IErrorObserver* pObserver
//) :
//    m_hTerminateEvent(TRUE, FALSE),
//    m_hExceptionEvent(TRUE, FALSE),
//    m_pCallback(pCallback),
//    m_queueLock(FALSE, FALSE),
//    m_actionsQueue(m_queueLock),
//    m_pObserver(pObserver)
//{
//    Start();
//}
//
//CActionsQueuer::~CActionsQueuer()
//{
//    Stop();
//    SAFE_WAIT(m_hActionsThread, m_pObserver);
//}
//
//void CActionsQueuer::Push(rdp_lib::Buffer* packet)
//{
//    if (::WaitForSingleObject(m_hTerminateEvent, 0) == WAIT_OBJECT_0)
//        throw std::runtime_error(std::string(__FUNCTION__) + " Error: actions queuer terminated");
//
//    m_actionsQueue.push_back_swap(*packet);
//}
//
//void CActionsQueuer::Start()
//{
//    m_hTerminateEvent.Reset();
//    
//    m_hActionsThread.Attach(reinterpret_cast<HANDLE>(::_beginthreadex(
//        NULL,
//        0,
//        ActionsThreadFunc,
//        this,
//        0,
//        NULL)));
//
//    if (m_hActionsThread == NULL)
//        throw std::runtime_error(std::string(__FUNCTION__) + " Failed to start thread");
//}
//
//void CActionsQueuer::Stop()
//{
//    m_hTerminateEvent.Set();
//}
//
//void CActionsQueuer::StopAndClear()
//{
//    Stop();
//    m_actionsQueue.clear();
//    m_queueLock.Reset();
//}
